package com.huatai.data.dal;

import com.google.common.collect.Maps;
import com.huatai.common.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Component
public class Dbconfig {

    private static final Logger logger = LoggerFactory.getLogger(Dbconfig.class);

    public static final String FATADB = "jdbc:oracle:thin:@10.250.66.11:8001:fatadb";

    public static final String TRADEDB = "jdbc:oracle:thin:@10.250.66.10:8000:tradedb";

    public static final String XRISK = "jdbc:oracle:thin:@10.250.20.38:5003:xrisk";

    private static final String configFilePath = "H:/外部数据/dbconfig.ini";

    private static final String USERID = "USERID=";

    private static final String PASSWORD = "PASSWORD=";

    private static Map<String, String> map = Maps.newHashMap();

    public static String getPasswordByUserId(String userId) {
        return map.get(userId);
    }

    @PostConstruct
    public void run() {
        logger.info("dbconfig.ini 读取任务开始......");
        // File f = new File("C:/dbconfig.ini");
        File f = new File(configFilePath);
        if (!f.exists()) {
            logger.info(configFilePath + "文件不存在");
            return;
        }
        try {
            List<String> strs = FileUtils.readLines(f);

            for (int i = 0; i < strs.size(); i++) {
                if (strs.get(i).indexOf(USERID) >= 0) {
                    String userId = strs.get(i).replace(USERID, "");
                    if (strs.get(i + 1).indexOf(PASSWORD) >= 0) {
                        String password = strs.get(i + 1).replace(PASSWORD, "");
                        logger.info("得到用户名【{}】的密码", userId);
                        map.put(userId, password);
                    }

                }
            }
        } catch (IOException e) {
            logger.error("dbconfig.ini 文件读取失败", e);
        }
        logger.info("dbconfig.ini 读取结束");
    }

}
